local battleSceneLoader = require "Battle/BattleSceneLoader"
BattleCurtainUI = { }
local this = BattleCurtainUI

function BattleCurtainUI:StartCurtain(startCallback)
    self.startCallback = startCallback
    self.passTime = 0
    self.speedX = BattleConfigData.curtain.width / BattleConfigData.curtain.startTime
    self:Load();
    UpdateBeat:Add(self.StartUpdate, self)
end

function BattleCurtainUI:Load(args)
    if (not self.curtain) then
        self.curtain = { }
        self.curtain.gameObject = battleSceneLoader.loadUI("BattleCurtainUI")
        self.curtain.leftDoor = UnityUtil.GetChildBone(self.curtain.gameObject.transform, "leftDoor")
        self.curtain.leftDoorAnchoredPosition3D = self.curtain.leftDoor.anchoredPosition3D
        self.curtain.rightDoor = UnityUtil.GetChildBone(self.curtain.gameObject.transform, "rightDoor")
        self.curtain.rightDoorAnchoredPosition3D = self.curtain.rightDoor.anchoredPosition3D
    end
    self.curtain.gameObject:SetActive(true)
    self:FreshPosition()
end

function BattleCurtainUI:StartUpdate()
    self.passTime = self.passTime + Time.deltaTime
    if (self.passTime >= BattleConfigData.curtain.startTime) then
        self.curtain.gameObject:SetActive(false)
        UpdateBeat:Remove(self.StartUpdate, self)
        self.startCallback()
        self.startCallback = nil
    end
    self:FreshPosition()
end


function BattleCurtainUI:EndCurtain(endCallback)
    self.endCallback = endCallback
    self.passTime = BattleConfigData.curtain.endTime
    self.speedX = BattleConfigData.curtain.width / BattleConfigData.curtain.endTime
    self:Load();
    UpdateBeat:Add(self.EndUpdate, self)
end

function BattleCurtainUI:EndUpdate()
    self.passTime = self.passTime - Time.deltaTime
    if (self.passTime <= 0) then
        self.passTime = 0
        UpdateBeat:Remove(self.EndUpdate, self)
        self.endCallback()
        self.endCallback = nil
    end
    self:FreshPosition()
end

function BattleCurtainUI:FreshPosition()
    self.curtain.leftDoorAnchoredPosition3D.x = - self.speedX * self.passTime
    self.curtain.rightDoorAnchoredPosition3D.x = self.speedX * self.passTime
    self.curtain.leftDoor.anchoredPosition3D = self.curtain.leftDoorAnchoredPosition3D
    self.curtain.rightDoor.anchoredPosition3D = self.curtain.rightDoorAnchoredPosition3D
end

function BattleCurtainUI.clear()
    this.curtain = nil
    this.passTime = nil
end


